package com.mingyuan.library.domain.lending.book.model.entity;

import com.mingyuan.library.domain.common.model.valueobject.Version;
import com.mingyuan.library.domain.common.model.Entity;
import com.mingyuan.library.domain.lending.book.model.valueobject.BookId;
import com.mingyuan.library.domain.lending.book.model.valueobject.BookInformation;
import com.mingyuan.library.domain.lending.book.model.valueobject.BookType;
import com.mingyuan.library.domain.lending.librarybranch.model.valueobject.LibraryBranchId;
import com.mingyuan.library.domain.lending.patron.event.BookCheckedOut;
import com.mingyuan.library.domain.lending.patron.event.BookHoldCanceled;
import com.mingyuan.library.domain.lending.patron.event.BookHoldExpired;
import com.mingyuan.library.domain.lending.patron.event.BookReturned;
import com.mingyuan.library.domain.lending.patron.model.valueobject.PatronId;
import lombok.*;

import java.time.Instant;

/**
 * 预定的图书
 * @author yangzk
 * @date 2023/12/28 19:20
 **/
@Value
@Entity
@AllArgsConstructor
@EqualsAndHashCode(of = "bookInformation")
public class BookOnHold implements Book {
    /**
     * 图书档案信息
     */
    @NonNull
    BookInformation bookInformation;
    /**
     * 图书馆分馆ID
     */
    @NonNull
    LibraryBranchId holdPlacedAt;
    /**
     * 读者ID
     */
    @NonNull
    PatronId byPatron;
    /**
     * 预定截止时间(预定到什么时间)
     */
    @NonNull
    Instant holdTill;
    /**
     * 版本号
     */
    @NonNull
    Version version;

    public BookOnHold(BookId bookId, BookType type, LibraryBranchId libraryBranchId, PatronId patronId, Instant holdTill, Version version) {
        this(new BookInformation(bookId, type), libraryBranchId, patronId, holdTill, version);
    }

    public AvailableBook handle(BookReturned bookReturned) {
        return new AvailableBook(
                bookInformation, new LibraryBranchId(bookReturned.getLibraryBranchId()),
                version);
    }

    public AvailableBook handle(BookHoldExpired bookHoldExpired) {
        return new AvailableBook(
                bookInformation,
                new LibraryBranchId(bookHoldExpired.getLibraryBranchId()),
                version);
    }

    public CheckedOutBook handle(BookCheckedOut bookCheckedOut) {
        return new CheckedOutBook(
                bookInformation,
                new LibraryBranchId(bookCheckedOut.getLibraryBranchId()),
                new PatronId(bookCheckedOut.getPatronId()),
                version);
    }

    public AvailableBook handle(BookHoldCanceled bookHoldCanceled) {
        return new AvailableBook(
                bookInformation, new LibraryBranchId(bookHoldCanceled.getLibraryBranchId()),
                version);
    }

    /**
     * 获取图书ID
     * @author yangzk
     * @date 2023/12/28 19:24
     * @return 图书ID
     **/
    public BookId getBookId() {
        return bookInformation.getBookId();
    }
    /**
     * 是否是当前读者
     * @author yangzk
     * @param patronId 读者ID
     * @date 2023/12/28 19:24
     **/
    public boolean by(PatronId patronId) {
        return byPatron.equals(patronId);
    }

}

